﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="LoadPoolsCommand.cs" company="CET">
//   The Center of Educational Technology
// </copyright>
// <summary>
//   Defines the LoadPoolsCommand type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace F5.BigIp.LocalTrafficManager.UI.Commands
{
    using System;
    using System.ComponentModel;
    using System.Windows.Input;

    using F5.BigIp.LocalTrafficManager.Interfaces;
    using F5.BigIp.LocalTrafficManager.Proxy.V11;
    using F5.BigIp.LocalTrafficManager.UI.ViewModels;

    /// <summary>
    /// The load pools command.
    /// </summary>
    public class LoadPools : ICommand
    {
        /// <summary>
        /// The view model.
        /// </summary>
        private readonly MainViewModel viewModel;

        /// <summary>
        /// Initializes a new instance of the <see cref="LoadPools"/> class.
        /// </summary>
        /// <param name="viewModel">
        /// The view model.
        /// </param>
        public LoadPools(MainViewModel viewModel)
        {
            if (viewModel == null)
            {
                throw new ArgumentNullException("viewModel");
            }

            this.viewModel = viewModel;
        }

        /// <summary>
        /// The can execute changed.
        /// </summary>
        public event EventHandler CanExecuteChanged;

        /// <summary>
        /// The can execute.
        /// </summary>
        /// <param name="parameter">
        /// The parameter.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool CanExecute(object parameter)
        {
            var device = parameter as LoadBalancer;

            return device != null &&
                !string.IsNullOrEmpty(device.Host) &&
                !string.IsNullOrEmpty(device.UserName) &&
                !string.IsNullOrEmpty(device.Password);
        }

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="parameter">
        /// The parameter.
        /// </param>
        public void Execute(object parameter)
        {
            var loadBalancer = parameter as LoadBalancer;

            if (loadBalancer == null)
            {
                throw new ArgumentException("LoadBalancer");
            }

            var backgroundWorker = new BackgroundWorker();

            backgroundWorker.DoWork += (s, e) =>
                {
                    this.viewModel.IsBusy = true;
                    this.viewModel.Status = "Loading pools...";

                    var poolService = new PoolService();

                    viewModel.Pools = poolService.GetPools(loadBalancer);
                };

            backgroundWorker.RunWorkerCompleted += (s, e) =>
                {
                    if (e.Error != null)
                    {
                        this.viewModel.Status = "Failed to load pools.";

                        this.viewModel.IsBusy = false;

                        return;
                    }

                    var worker = s as BackgroundWorker;

                    if (worker == null)
                    {
                        return;
                    }

                    this.viewModel.Status = loadBalancer.Version;
                    this.viewModel.IsBusy = false;
                };

            backgroundWorker.RunWorkerAsync();
        }
    }
}
